/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import domain.DBConnection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author admin
 */
public class LoginPersistence {
    
    /**
     * Retrieves the password for a given username
     * @param username for which the password is required
     * @return  the password
     */
    public String getPassword(String username){
        DBConnection dbCon = DBConnection.getDatabaseConn();
        dbCon.connect();
        String retreiveSQL = "SELECT `password` from users WHERE `username` = '"+username+"';";
        ResultSet rs = dbCon.fetch(retreiveSQL);
        try {
            if(rs.first()){
            return(rs.getString(1));
            }else{
                return null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * Retrieve the user_id for a given username
     * @param username
     * @return the user_id
     */
    public int getUserId(String username) {
        int userID = -1;
        String fetchSQL = "SELECT `user_id` from users WHERE `username` = '"+username+"';";
        DBConnection dbConn = DBConnection.getDatabaseConn();
        dbConn.connect();
        ResultSet rs = dbConn.fetch(fetchSQL);
        try {
            if(rs.next()){
                userID =  rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userID;
    }
}
